package com.learn.demo.lambda;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * lambda表达式，业务去重并融合
 *
 * @author banjiawei
 * @date 2021/06/19
 */
@Slf4j
public class MergeAndDistinctDemo {
    public static void main(String[] args) {
        Map<String, Order> orderMap = new HashMap<>();
        MergeAndDistinctDemo demo = new MergeAndDistinctDemo();
        //匿名非静态内部类的创建
        Order order1 = demo.new Order();
        //没有规格的商品
        Good good1 = demo.new Good();
        good1.setGoodName("小米10");
        good1.setSaleCount(1);
        Good good2 = demo.new Good();
        good2.setGoodName("小米10");
        good2.setSaleCount(2);
        List<Good> goodList = new ArrayList<>();
        goodList.add(good1);
        goodList.add(good2);
        order1.setOrderId("order123");
        order1.setGoodList(goodList);
        log.info("=========================合并之前，{}=========================", goodList);
        ArrayList<Good> goods = new ArrayList<>(order1.getGoodList().stream().collect(Collectors.toMap(Good::getGoodName, item -> item, (item1, item2) -> {
            item1.setSaleCount(item1.getSaleCount() + item2.getSaleCount());
            return item1;
        })).values());
        log.info("=========================合并之后，{}=========================", goods);
    }

    public static void testMapMerge(Map<String, Order> map) {
    }

    /**
     * 订单类
     */
    @Data
    public class Order {
        /*订单编号*/
        private String orderId;
        /*商品列表*/
        private List<Good> goodList;
    }

    /**
     * 商品类
     */
    @Data
    public class Good {
        /*商品名称*/
        private String goodName;
        /*销售数量*/
        private Integer saleCount;
        /*规格列表*/
        private List<Specs> specsList;
    }

    /**
     * 规格类
     */
    @Data
    public class Specs {
        /*规格名称*/
        private String specsName;
        /*规格描述*/
        private String specsDesc;
    }
}
